package com.ruoyi.manage.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.DistanceUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.City;
import com.ruoyi.manage.domain.Jurisdiction;
import com.ruoyi.manage.domain.JurisdictionScope;
import com.ruoyi.manage.domain.LocationAll;
import com.ruoyi.manage.domain.request.AreaDetailsRequest;
import com.ruoyi.manage.domain.request.LocationRequest;
import com.ruoyi.manage.mapper.JurisdictionMapper;
import com.ruoyi.manage.mapper.JurisdictionScopeMapper;
import com.ruoyi.manage.mapper.LocationAllMapper;
import com.ruoyi.manage.service.IAppLocationAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2021/1/7 11:13
 */
@Service
public class AppLocationAllServiceImpl implements IAppLocationAllService {
    @Value("${maxKill}")
    private double maxKill;
    @Autowired
    private LocationAllMapper locationAllMapper;
    @Autowired
    private JurisdictionMapper jurisdictionMapper;
    @Autowired
    private JurisdictionScopeMapper jurisdictionScopeMapper;
    @Override
    public AjaxResult getLocation(LocationRequest locationRequest) {
        if(locationRequest == null || locationRequest.getLat() == null || locationRequest.getLon() == null || StringUtils.isEmpty(locationRequest.getCity())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }

        LocationAll location = locationAllMapper.getLocation(locationRequest);
        if(location == null){
            throw new CustomException("查找不到信息", HttpStatus.ERROR);
        }
        double distance = DistanceUtils.Algorithm(locationRequest.getLon(), locationRequest.getLat(), location.getLongNumber(), location.getLatNumber());

        if(distance >= maxKill){
            throw new CustomException("查找不到信息", HttpStatus.ERROR);
        }
        //根据公里数，高速名称，城市查出所在的辖区
        String kilometers = location.getKilometers().replace("K","").replace("k","");
        String speed = location.getSpeed();
        String expressway1 = DictUtils.getDictValue("expressway", speed);

        String city_id = DictUtils.getDictValue("hn_city", locationRequest.getCity());
        //判断该城市存在几个辖区，如果存在一个，直接获取辖区，存在多个辖区，进行高速名称和公里数获取辖区
        List<Jurisdiction> jurisdictionList = jurisdictionMapper.findJurisdictionListByCityId(city_id);

        String jurisdictionName = null;
        Long jurisdiction1_id = null;
        if(jurisdictionList.size()<=1){
            //存在一个辖区，直接返回数据
            if(jurisdictionList.size()<=0){
                throw new CustomException("查找不到信息", HttpStatus.ERROR);
            }
            Jurisdiction jurisdiction = jurisdictionList.get(0);
            jurisdictionName = jurisdiction.getJurisdictionName();
            jurisdiction1_id =jurisdiction.getId();
        }else{
            //多个辖区，进行高速名称和公里数获取辖区
            List<JurisdictionScope> jurisdictionScopes = jurisdictionScopeMapper.selectJurisdictionScopeByNameAndKil(expressway1, kilometers);
            if(jurisdictionScopes.size()<=0){
                throw new CustomException("查找不到信息", HttpStatus.ERROR);
            }
            JurisdictionScope jurisdictionScope = jurisdictionScopes.get(0);
            Jurisdiction jurisdiction1 = jurisdictionMapper.selectJurisdictionById(jurisdictionScope.getJurisdictionId());
            jurisdictionName = jurisdiction1.getJurisdictionName();
            jurisdiction1_id = jurisdiction1.getId();
        }
        if(StringUtils.isEmpty(jurisdictionName)){
            throw new CustomException("查找不到信息", HttpStatus.ERROR);
        }
        location.setArea(jurisdictionName);
        location.setJurisdictionId(jurisdiction1_id);
        return AjaxResult.success(location);
    }

    @Override
    public AjaxResult getCity() {
        List<City>  city = locationAllMapper.getCity();
        List<String> cityStr = new ArrayList<>();
        for (City city1 : city) {
            cityStr.add(city1.getText());
        }
        return AjaxResult.success(cityStr);
    }

    @Override
    public AjaxResult getArea(LocationRequest locationRequest) {
        if(locationRequest == null || StringUtils.isEmpty(locationRequest.getCity()) || StringUtils.isEmpty(locationRequest.getStreet())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        List<City> areaByCity = locationAllMapper.getAreaByCity(locationRequest);
        List<String> cityStr = new ArrayList<>();
        for (City city : areaByCity) {
            cityStr.add(city.getText());
        }
        return AjaxResult.success(cityStr);
    }

    @Override
    public AjaxResult getStreet(LocationRequest locationRequest) {
        if(locationRequest == null || StringUtils.isEmpty(locationRequest.getCity())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        List<City>  streetByCity = locationAllMapper.getStreetByCity(locationRequest);
        List<String> cityStr = new ArrayList<>();
        for (City city1 : streetByCity) {
            cityStr.add(city1.getText());
        }
        return AjaxResult.success(cityStr);
    }

    @Override
    public AjaxResult getCityNew() {
        List<SysDictData> hn_city_list = DictUtils.getDictCache("hn_city");
        List<City> cityList = new ArrayList<>();
        for (SysDictData sysDictData : hn_city_list) {
            City city = new City();
            String dictLabel = sysDictData.getDictLabel();
            String dictValue = sysDictData.getDictValue();
            city.setId(dictValue);
            city.setText(dictLabel);
            cityList.add(city);
        }
        return AjaxResult.success(cityList);
    }

    /**
     * 新版查询
     * 根据city_id查询出该城市下面的所有辖区，
     * 根据辖区id查询出该城市下的所有高速
     * @param locationRequest
     * @return
     */
    @Override
    public AjaxResult getStreetNew(LocationRequest locationRequest) {
        if(locationRequest == null || StringUtils.isEmpty(locationRequest.getCity())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        String city_id = locationRequest.getCity();
        List<Jurisdiction> jurisdictionList = jurisdictionMapper.findJurisdictionListByCityId(city_id);
        Long[] jurisdictionIdArray = new Long[jurisdictionList.size()];
        for (int i = 0; i < jurisdictionList.size(); i++) {
            jurisdictionIdArray[i] = jurisdictionList.get(i).getId();
        }
        List<City> cityList = new ArrayList<>();
        if(jurisdictionIdArray == null || jurisdictionIdArray.length<=0){
            return AjaxResult.success(cityList);
        }

        List<JurisdictionScope> jurisdictionScopes = jurisdictionScopeMapper.selectJurisdictionScopeListByJurisdictionIds(jurisdictionIdArray);

        for (JurisdictionScope jurisdictionScope : jurisdictionScopes) {
            City city = new City();
            city.setId(jurisdictionScope.getExpresswayName());
            city.setText(DictUtils.getDictLabel("expressway",jurisdictionScope.getExpresswayName()));
            cityList.add(city);
        }
        return AjaxResult.success(cityList);
    }

    /**
     * 获取辖区新版
     * @param locationRequest
     * @return
     */
    @Override
    public AjaxResult getAreaNew(LocationRequest locationRequest) {
        if(locationRequest == null || StringUtils.isEmpty(locationRequest.getCity())|| StringUtils.isEmpty(locationRequest.getStreet())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        String city_id = locationRequest.getCity();

        List<Jurisdiction> jurisdictionList1 = jurisdictionScopeMapper.selectJurisdictionScopeByCityIdAndExpresswayName(city_id, locationRequest.getStreet());
        List<City> cityList = new ArrayList<>();
        for (Jurisdiction jurisdiction : jurisdictionList1) {
            City city = new City();
            city.setId(jurisdiction.getId()+"");
            city.setText(jurisdiction.getJurisdictionName());
            cityList.add(city);
        }
        return AjaxResult.success(cityList);
    }

    @Override
    public AjaxResult getAreaDetails(AreaDetailsRequest areaDetailsRequest) {
        List<JurisdictionScope> jurisdictionScopes = jurisdictionScopeMapper.selectJurisdictionScopeByNameAndKil(areaDetailsRequest.getExpresswayId(), String.valueOf(areaDetailsRequest.getKil()));
        if(jurisdictionScopes == null || jurisdictionScopes.size()<=0){
            throw new CustomException("查找不到信息", HttpStatus.ERROR);
        }
        JurisdictionScope jurisdictionScope = jurisdictionScopes.get(0);
        Jurisdiction jurisdiction1 = jurisdictionMapper.selectJurisdictionById(jurisdictionScope.getJurisdictionId());
        if(jurisdictionMapper == null){
            throw new CustomException("查找不到信息", HttpStatus.ERROR);
        }
        return AjaxResult.success(jurisdiction1);
    }


}
